package com.idanchuang.database.manage.controller;

import com.idanchuang.component.base.JsonResult;
import com.idanchuang.component.base.page.PageData;
import com.idanchuang.database.manage.model.MenuList;
import com.idanchuang.database.manage.model.PageInput;
import com.idanchuang.database.manage.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @param
 * @author:zhangmengsi
 * @description:
 * @date:Created in 17:00 2021/2/6
 */
@Controller
public class CrudController {

    @Autowired
    private CrudService crudService;

    @ResponseBody
    @GetMapping("getMenuList")
    public JsonResult<List<MenuList>> getMenuList() {
        List<MenuList> menuLists = crudService.getMenuList();
        JsonResult<List<MenuList>> result = JsonResult.success();
        result.setData(menuLists);
        return result;
    }


    @ResponseBody
    @GetMapping("getTableInfo")
    public JsonResult<PageData<Map<String, Object>>> getAllTableInfo(@RequestParam Integer current, @RequestParam Integer size, @RequestParam String database, @RequestParam String table) {
        PageData<Map<String, Object>> listPageData = crudService.getAllTableInfo(current,size,database,table);
        JsonResult<PageData<Map<String, Object>>> result = JsonResult.success();
        result.setData(listPageData);
        return result;
    }

    @ResponseBody
    @GetMapping("getField")
    public JsonResult<List<Map<String, Object>>> getAllField(String database,String tableName) {
        List<Map<String, Object>> listPageData = crudService.getAllField(database,tableName);
        JsonResult<List<Map<String, Object>>> result = JsonResult.success();
        result.setData(listPageData);
        return result;
    }

    //通过sql语句查询所需要的信息
    @ResponseBody
    @PostMapping("/executeSql")
    public JsonResult<Object> executeSql(@RequestParam String database, @RequestParam String
            sql, @RequestBody PageInput input) {
        JsonResult<Object> res = crudService.executeSql(database,sql,input);
        return res;
    }
}


